package org.wxy.sdk.base.impl;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.IBaseView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
public abstract class BaseFragment extends Fragment implements IBaseView {
    private IBaseView mBaseView;
    private View mContentView;
    private Unbinder mUnbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflateView(inflater, container, savedInstanceState);
            if (mContentView == null) {
                return super.onCreateView(inflater, container, savedInstanceState);
            }
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -1);
            mContentView.setLayoutParams(params);
        }
        EventBus.getDefault().register(this);
        return mContentView;
    }

    protected abstract View inflateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState);
    protected abstract void initViews();
    protected abstract void initVariables();
    protected abstract void loadData();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mBaseView = new BaseViewImpl(context, getClass().getSimpleName());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnbinder = ButterKnife.bind(this, mContentView);
        initVariables();
        initViews();
        loadData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
        mUnbinder = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showToast(String tip) {
        if (mBaseView != null) {
            mBaseView.showToast(tip);
        }
    }

    @Override
    public void showToast(@StringRes int strResId) {
        if (mBaseView != null) {
            mBaseView.showToast(strResId);
        }
    }

    @Override
    public void showLoadingDialog() {
        mBaseView.showLoadingDialog();
    }

    @Override
    public void showLoadingDialog(String msg) {
        mBaseView.showLoadingDialog(msg);
    }

    @Override
    public void hideLoadingDialog() {
        if (mBaseView != null) {
            mBaseView.hideLoadingDialog();
        }
    }

    @Override
    public void showLoadingDialog(@StringRes int strResId) {
        if (mBaseView != null) {
            mBaseView.showLoadingDialog(strResId);
        }
    }

    @Override
    public void showError(String message) {
        if (mBaseView != null) {
            mBaseView.showError(message);
        }
    }

    @Override
    public void showError(Throwable throwable, String message) {
        if (mBaseView != null) {
            mBaseView.showError(throwable, message);
        }
    }

    @Override
    public void showError(@StringRes int strResId) {
        if (mBaseView != null) {
            mBaseView.showError(strResId);
        }
    }

    @Override
    public void showError(String title, String message) {
        if (mBaseView != null) {
            mBaseView.showError(title, message);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BaseFragment baseFragment) {

    }

    @Override
    public void navigation(String path) {
        if (mBaseView != null) {
            mBaseView.navigation(path);
        }
    }

    @Override
    public void navigation(String path, Bundle bundle) {
        if (mBaseView != null) {
            mBaseView.navigation(path, bundle);
        }
    }

    @Override
    public void navigation(String path, int requestCode) {
        if (mBaseView != null) {
            mBaseView.navigation(path, requestCode);
        }
    }

    @Override
    public void navigation(String path, Bundle bundle, int requestCode) {
        if (mBaseView != null) {
            mBaseView.navigation(path, bundle, requestCode);
        }
    }

    @Override
    public String getStringRes(@StringRes int strResId, Object... objects) {
        if (mBaseView != null) {
            return mBaseView.getStringRes(strResId, objects);
        }
        return null;
    }

    @Override
    public Context getContext() {
        if (mBaseView != null) {
            return mBaseView.getContext();
        }
        return null;
    }

    @Override
    public void destroy() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onDetach() {
        destroy();
        super.onDetach();
        mBaseView.destroy();
        mBaseView = null;
        IBasePresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.unsubscribe();
        }
    }


}
